//
//  userInfoController.m
//  SSSAllRollAgency
//
//  Created by Darker on 2017/3/15.
//  Copyright © 2017年 Darker. All rights reserved.
//

#import "userInfoController.h"
#import "exchangeDetailCell.h"
#import "userInfoModel.h"
#import "NSDictionaryAdditions.h"

@interface userInfoController ()<UITableViewDelegate,UITableViewDataSource>

{
    
    NSArray *_dataArray;
    UIImageView *_bgImageView;//背景图
    NSArray *_descArray;
    UITableView *_listTableView;
    userInfoModel *model;
}

@end
static NSString * const userInfoID = @"userInfoCell";

@implementation userInfoController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.title = @"用户信息";
    //去掉透明后导航栏下边的黑边
    //    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = [UIColor colorWithHex:@"F9F9FA"];
    //[QMUITips showLoading:@"加载中..." detailText:nil inView:self.view];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //请求用户数据
    [self reloadData];
    
//    _dataArray = @[@"姓名",@"加入时间",@"手机号"];
//    _descArray = @[@"张三",@"2017-03-09 10:09:07",@"18080087309"];
    
    //添加背景图
    UIImageView *bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg_1"]];
    bgImageView.userInteractionEnabled = YES;
    [self.view addSubview:bgImageView];
    bgImageView.frame = self.view.frame;
    _bgImageView = bgImageView;
    
     [self initListTableView];
    
}
- (void)initListTableView{
    
    _listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, FSCREEN_Width, FSCREEN_Height) style:UITableViewStylePlain];
    _listTableView.backgroundColor = [UIColor clearColor];
    _listTableView.bounces = NO;
    _listTableView.delegate = self;
    _listTableView.dataSource = self;
    _listTableView.rowHeight = HeigthIos6CompeteWithCurrent(51);
    _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_bgImageView addSubview:_listTableView];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Class currentClass = [exchangeDetailCell class];
    
    exchangeDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(currentClass)];
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (!cell) {
        cell = [[exchangeDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:userInfoID];
    }
    if (indexPath.row == _dataArray.count - 1) {
        cell.lineView.hidden = YES;
    }
    //cell.nameLabel.text = [NSString stringWithFormat:@"%@",_mDelegateArray[indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.nameLabel.text = _dataArray[indexPath.row];
    cell.descLabel.text = _descArray[indexPath.row];
    return cell;
}
- (void)reloadData{
    
    //NSString *UserInfoUrl = [NSString stringWithFormat:@"%@%@",SSSCommomUrl,@"user/detail"];
    
    
    //[QMUITips showLoading:@"加载中..." inView:_bgImageView hideAfterDelay:2];
    //[SVProgressHUD showInfoWithStatus:@"加载中"];
    
    
    [[XHRequestManager manager] getRequest:kConfigPath(kUserDetail) params:nil success:^(id  _Nonnull responseObj) {
        
        XHLog(@"userInfo--- %@",responseObj);
        
        model = [userInfoModel modelWithJSON:responseObj[@"data"]];
        XHLog (@"---model.createTime---%@",model.createTime);
        
        if ([responseObj[@"status"] intValue] == 0) {
            
            if (![responseObj objectOrNilForKey:@"data"]) {
                
                [QMUITips showError:@"用户数据为空" inView:self.view hideAfterDelay:2];
                return ;
                
            }else{
                
                [QMUITips showSucceed:@"加载成功" inView:self.view hideAfterDelay:0.5];
                [QMUITips hideAllToastInView:self.view animated:YES];
                
                //[self hideEmptyView];
                dispatch_async(dispatch_get_main_queue(), ^{
                    _dataArray = @[@"姓名",@"手机号",@"创建时间"];
                    NSString *subStr = [model.createTime xhstringWithFormat:@"yyyy-MM-dd"];
                    
                    //NSString *timeStr = [NSString timeDateStrFromTimeStamp:subStr];
                    
                    _descArray = @[model.name,model.phone,subStr];
                    XHLog(@"--------%@",_descArray);
                    //刷新表格
                    [_listTableView reloadData];
                });
            }
            
        }else{
            
            [QMUITips hideAllToastInView:self.view animated:YES];
            [QMUITips showError:responseObj[@"message"] inView:self.view hideAfterDelay:2];
        }

        
    } failure:^(NSError * _Nonnull error) {
        
        [QMUITips hideAllToastInView:self.view animated:YES];
        XHLog(@"error -- %@",error.localizedDescription);
        [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
